package com.navinfo.data.esper.cep.linkid;


import com.espertech.esper.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class LinkidMainJavaPattern {
    private static final Logger log = LoggerFactory.getLogger(LinkidMainJavaPattern.class);

    /**
     * 获取包含同样次序linkid序列的tid列表
     * @param map tid及其linkid列表
     * @param roadLinkids 待查的次序linkid列表
     * @return 满足条件的tid列表
     */
    public static List<Integer> getLinkidList(Map<Integer, List<Integer>> map,List<Integer> roadLinkids) {
        List<Integer> tidList=new ArrayList<Integer>(300000);

        int roadlinkid = 0;
        StringBuffer sb = new StringBuffer().append("(.*)");
//        (.*)(#200000#)(.*)(#200001#)(.*)(#200002#)(.*)
        for(int z=0;z<roadLinkids.size();z++){
            sb.append("(#").append(roadLinkids.get(z)).append("#)(.*)");
        }

        for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
//            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            StringBuffer llist = new StringBuffer().append("");
            for (int j = 0; j < entry.getValue().size(); j++) {
                llist.append("#").append(entry.getValue().get(j)).append("#");
            }
            if (Pattern.matches(sb.toString(), llist.toString())) {
                tidList.add(entry.getKey());
            }
        }
        Collections.sort(tidList);
        return tidList;

    }

    public static void main(String[] args) throws IOException, InterruptedException {

        log.info("making test data begin......");
        //创建测试数据集
        Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>(1000000);
        for (int i = 700001; i < 800110; i++) {
            List<Integer> li = new ArrayList<Integer>(3000);
            for (int j = 1; j < 2400; j++) {
                li.add(j+901099-i);
            }
            map.put(i,li);
        }

        List<Integer> roadLinkids=new ArrayList<Integer>(100);
        for (int j = 1; j < 10; j++) {
            roadLinkids.add(200000-1+j);
        }

        log.info("making test data end......");

        log.info("getting tid list begin ......");
        List<Integer> unique= getLinkidList(map,roadLinkids);
        log.info("getting tid list end ......");

        log.info("printing tid list begin ......");
        Iterator<Integer> it2 = unique.iterator();
        while (it2.hasNext()) {
            log.info(it2.next() + " ");
        }
        log.info("printing tid list end ......");
    }
}
